<template>
	<view class="content">
		<view class="layout">
			<view class="box" v-for="(item,index) in arrs" :key="item._id">
				<view class="pic">
					<image lazy-load :src="item.url" mode="widthFix" @click="preView(index)"></image>
				</view>
				<view class="text">{{item.content}}</view>
				<view class="author">——{{item.author}}</view>
			</view>
		</view>
		<view class="float">
			<view class="refresh" @click="refresh">
				<uni-icons type="refreshempty" size="24"></uni-icons>
			</view>
			<view class="top" @click="top">
				<uni-icons type="arrow-up" size="24"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script setup>
import {ref} from 'vue'
import {onReachBottom,onPullDownRefresh} from '@dcloudio/uni-app'
const arrs = ref([]);


//图片预览
const preView = function(index){
	let urls = arrs.value.map(item=>item.url);
	console.log(urls);
	uni.previewImage({
		current:index,
		urls
	})
}

//刷新
const refresh = function(){
	arrs.value = [];
	getData();
}

//置顶
const top = function(){
	uni.pageScrollTo({
		scrollTop:0,
		duration:100		
	})
}

//获取网络数据	
function getData(){
	//标题栏显示加载
	uni.showNavigationBarLoading();
	uni.request({
		url:'https://tea.qingnian8.com/tools/petShow',
		data:{
			size:5
		},
		header:{
			"access-key":"815190"
		}
	}).then(res=>{
		if(res.data.errCode === 0){
			
			arrs.value = [ ...arrs.value,...res.data.data ];
			console.log(arrs.value);
			
		}else if(res.data.errCode ===400){
			uni.showToast({
				title:res.data.errMsg,
				icon:'none'
			})
		}
	}).catch(err=>{
		uni.showToast({
			title:'请求错误',
			icon:'none'
		})
	}).finally(()=>{
		//不管成功与否，都关闭标题栏加载
		uni.hideNavigationBarLoading();
		uni.stopPullDownRefresh()
	})
}

//触底加载更多
onReachBottom(()=>{
	getData();
})

//下拉刷新
onPullDownRefresh(()=>{
	arrs.value = [];
	getData();
})



getData();
</script>

<style lang="scss">
	.content{
		.layout{
			padding: 40rpx;
			.box{
				margin-bottom: 60rpx;
				border-radius: 20rpx;
				overflow: hidden;
				box-shadow: 0 10rpx 40rpx rgba(0,0,0,0.05);
				.pic{
					image{
						width: 100%;
					}
				}
				.text{
					padding: 30rpx;
					font-size: 36rpx;
					color: #a3a3a3;
				}
				.author{
					padding: 0 30rpx 30rpx;
					text-align: right;
					font-size: 24rpx;
					color: #c2c2c2;
				}
			}
		}
		.float{
			position: fixed;
			right: 20rpx;
			bottom: 80rpx;
			.refresh,.top{
				width: 100rpx;
				height: 100rpx;
				background-color: rgba(255,255,255,0.8);
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 20rpx;
			}
			
		}
	}
</style>
